package com.example.exercise.sys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.sys.vo.SysAdminRoleVO;
import com.example.exercise.sys.dao.entity.SysAdminRoleEntity;
import com.example.exercise.sys.dao.SysAdminRoleDAO;

/**
* 
*/
@Service
public class SysAdminRoleService {

    @Autowired
    private SysAdminRoleDAO sysAdminRoleDAO;

    public List<SysAdminRoleVO> findByWhere(Page<?> page, SysAdminRoleVO sysAdminRoleVO){
        List<SysAdminRoleVO> list = new ArrayList<>();
        List<SysAdminRoleEntity> list0 = sysAdminRoleDAO.findByWhere(page, sysAdminRoleVO);
        for (SysAdminRoleEntity sysAdminRoleEntity : list0) {
            list.add(toSysAdminRoleVO(sysAdminRoleEntity));
        }
        return list;
    }

    public SysAdminRoleVO findByKey(SysAdminRoleVO sysAdminRoleVO){
        SysAdminRoleEntity sysAdminRoleEntity = sysAdminRoleDAO.findByKey(sysAdminRoleVO);
        return toSysAdminRoleVO(sysAdminRoleEntity);
    }

    public int insert(SysAdminRoleVO sysAdminRoleVO){
        return sysAdminRoleDAO.insert(toSysAdminRoleEntity(sysAdminRoleVO));
    }

    public int updateByKey(SysAdminRoleVO sysAdminRoleVO){
        return sysAdminRoleDAO.updateByKey(toSysAdminRoleEntity(sysAdminRoleVO));
    }

    public int deleteByKey(SysAdminRoleVO sysAdminRoleVO){
        return sysAdminRoleDAO.deleteByKey(sysAdminRoleVO);
    }

    private SysAdminRoleVO toSysAdminRoleVO(SysAdminRoleEntity sysAdminRoleEntity) {
        SysAdminRoleVO sysAdminRoleVO = new SysAdminRoleVO();
                sysAdminRoleVO.setId(String.valueOf(sysAdminRoleEntity.getId()));
        sysAdminRoleVO.setUserId(String.valueOf(sysAdminRoleEntity.getUserId()));
        sysAdminRoleVO.setRoleId(String.valueOf(sysAdminRoleEntity.getRoleId()));

        return sysAdminRoleVO;
    }

    private SysAdminRoleEntity toSysAdminRoleEntity(SysAdminRoleVO sysAdminRoleVO) {
        SysAdminRoleEntity sysAdminRoleEntity = new SysAdminRoleEntity();
                String id = sysAdminRoleVO.getId();
        if(id != null) {
            sysAdminRoleEntity.setId(Integer.parseInt(sysAdminRoleVO.getId()));
        }
        String userId = sysAdminRoleVO.getUserId();
        if(userId != null) {
            sysAdminRoleEntity.setUserId(Integer.parseInt(sysAdminRoleVO.getUserId()));
        }
        String roleId = sysAdminRoleVO.getRoleId();
        if(roleId != null) {
            sysAdminRoleEntity.setRoleId(Integer.parseInt(sysAdminRoleVO.getRoleId()));
        }

        return sysAdminRoleEntity;
    }

}